//
//  CYMyFollowViewController.m
//  cyydt
//
//  Created by chu on 2019/1/24.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYMyFollowViewController.h"

#import "CYMyFollow_WenDaTableViewCell.h"
#import "CYMyFollow_ShiPinTableViewCell.h"
#import "CYMyFollow_WenZhangTableViewCell.h"
#import "CYMyFollow_XiangMuTableViewCell.h"
#import "CYMyFollow_NoFollowTableViewCell.h"
#import "CYMyFollow_TuiJianGuanZhuCollectionViewCell.h"


#import "CYArcitleDetailsViewController.h"
#import "CYHomeVideoDetailsViewController.h"
#import "CYGeRenZhuYeViewController.h"
#import "CYProjectDetailViewController.h"
#import "CYWenDaDetailViewController.h"

@interface CYMyFollowViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
{
    BOOL _noGuanZhu;
}
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@property (nonatomic, strong) NSMutableArray *dynamicsArr;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UIView *headView;

@end

@implementation CYMyFollowViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的关注";
    [self.view addSubview:self.tableView];
    [self getData];

    self.tableView.estimatedRowHeight = 44;
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dynamicsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_noGuanZhu) {
        CYMyFollow_NoFollowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYMyFollow_NoFollowTableViewCell"];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"CYMyFollow_NoFollowTableViewCell" owner:nil options:nil][0];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        CYProjectModel *model = self.dynamicsArr[indexPath.row];
        cell.model = self.dynamicsArr[indexPath.row];
        cell.block = ^{
            [self guanzhuWithId:model.userId];
        };
        return cell;
    }else{
        CYProjectModel *model = self.dynamicsArr[indexPath.row];
        if ([model.type integerValue] == 3 || [model.type integerValue] == 9) {
            //项目
            CYMyFollow_XiangMuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYMyFollow_XiangMuTableViewCell"];
            if (!cell) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CYMyFollow_XiangMuTableViewCell" owner:nil options:nil][0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }else if ([model.type integerValue] == 4){
            //问答
            CYMyFollow_WenDaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYMyFollow_WenDaTableViewCell"];
            if (!cell) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CYMyFollow_WenDaTableViewCell" owner:nil options:nil][0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = model;
            return cell;
        }else if ([model.type integerValue] == 7){
            //新闻

            CYMyFollow_WenZhangTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYMyFollow_WenZhangTableViewCell"];
            if (!cell) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CYMyFollow_WenZhangTableViewCell" owner:nil options:nil][0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = model;
            return cell;

        }else{
            //视频
            CYMyFollow_ShiPinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYMyFollow_ShiPinTableViewCell"];
            if (!cell) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CYMyFollow_ShiPinTableViewCell" owner:nil options:nil][0];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = model;
            return cell;
        }
        
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_noGuanZhu) {
        return 90;
    }
    CYProjectModel *model = self.dynamicsArr[indexPath.row];
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    CYProjectModel *model = self.dynamicsArr[indexPath.row];
    if (!_noGuanZhu) {
        if ([model.type integerValue] == 3 || [model.type integerValue] == 9) {
            //项目
            CYProjectDetailViewController *info = [[CYProjectDetailViewController alloc] init];
            info.model = model;
            [[AppDelegate sharedAppDelegate] pushViewController:info];
        }else if ([model.type integerValue] == 4){
            //问答
            CYWenDaDetailViewController *info = [[CYWenDaDetailViewController alloc] init];
            info.wedaId = [model.ID stringValue];
            [[AppDelegate sharedAppDelegate] pushViewController:info];
        }else if ([model.type integerValue] == 7){
            //新闻
            CYArcitleDetailsViewController *info = [[CYArcitleDetailsViewController alloc] init];
            info.articleId = [model.ID stringValue];
            [[AppDelegate sharedAppDelegate] pushViewController:info];
        }else{
            //视频
            CYHomeVideoDetailsViewController *info = [[CYHomeVideoDetailsViewController alloc] init];
            info.videoId = [model.ID stringValue];
            [[AppDelegate sharedAppDelegate] pushViewController:info];
        }
    }else{
        CYGeRenZhuYeViewController *zhuye = [[CYGeRenZhuYeViewController alloc] init];
        zhuye.userId = model.userId;
        [[AppDelegate sharedAppDelegate] pushViewController:zhuye];
    }
    
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CYProjectModel *model = self.dataSourceArr[indexPath.item];

    CYGeRenZhuYeViewController *zhuye = [[CYGeRenZhuYeViewController alloc] init];
    zhuye.userId = model.userId;
    [[AppDelegate sharedAppDelegate] pushViewController:zhuye];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CYMyFollow_TuiJianGuanZhuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CYMyFollow_TuiJianGuanZhuCollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[CYMyFollow_TuiJianGuanZhuCollectionViewCell alloc] init];
    }
    cell.model = self.dataSourceArr[indexPath.item];
    return cell;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(50, 85);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    
    return CGSizeZero;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(20, 35, 20, 35);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = nil;
    return view;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH - Height_NavBar) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackGroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.headView;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getData];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self getMoreData];
        }];
        
        [_tableView registerNib:[UINib nibWithNibName:@"CYMyFollow_WenDaTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYMyFollow_WenDaTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"CYMyFollow_ShiPinTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYMyFollow_ShiPinTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"CYMyFollow_WenZhangTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYMyFollow_WenZhangTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"CYMyFollow_XiangMuTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYMyFollow_XiangMuTableViewCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"CYMyFollow_NoFollowTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYMyFollow_NoFollowTableViewCell"];
        
    }
    return _tableView;
}

#pragma mark - 我的关注
- (void)getData{
    self.page = 1;
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"attention/attendDynamic"];
    NSDictionary *param = @{@"pageNo":@1, @"pageSize":@"10"};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"我的关注 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSDictionary *data = responseResult[@"data"];
                NSArray *itemUsers = data[@"itemUsers"];
                NSArray *dynamics = data[@"dynamics"];
                [self.dataSourceArr removeAllObjects];
                [self.dynamicsArr removeAllObjects];
                if (dynamics.count > 0) {
                    self->_noGuanZhu = NO;
                    for (NSDictionary *dic in dynamics) {
                        CYProjectModel *model = [CYProjectModel mj_objectWithKeyValues:dic];
                        [self.dynamicsArr addObject:model];
                    }
                    for (int i = 0; i < itemUsers.count; i++) {
                        NSDictionary *dic = itemUsers[i];
                        if (i < 10) {
                            CYProjectModel *model = [CYProjectModel mj_objectWithKeyValues:dic];
                            [self.dataSourceArr addObject:model];
                        }
                    }
                }else{
                    self->_noGuanZhu = YES;
                    for (int i = 0; i < itemUsers.count; i++) {
                        NSDictionary *dic = itemUsers[i];
                        if (i < 10) {
                            CYProjectModel *model = [CYProjectModel mj_objectWithKeyValues:dic];
                            [self.dataSourceArr addObject:model];
                        }else{
                            CYProjectModel *model = [CYProjectModel mj_objectWithKeyValues:dic];
                            [self.dynamicsArr addObject:model];
                        }
                    }
                }
                [self.tableView reloadData];
                [self.collectionView reloadData];
            }else{
                
            }
        }
    }];
}

#pragma mark - 我的关注
- (void)getMoreData{
    self.page ++;
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"attention/attendDynamic"];
    NSDictionary *param = @{@"pageNo":[NSNumber numberWithInteger:self.page], @"pageSize":@"10"};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"我的关注 ---- %@",responseResult);
        if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
            self.page --;
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                
                NSDictionary *data = responseResult[@"data"];
                NSArray *dynamics = data[@"dynamics"];
                if (self->_noGuanZhu) {
                    
                }else{
                    for (NSDictionary *dic in dynamics) {
                        CYProjectModel *model = [CYProjectModel mj_objectWithKeyValues:dic];
                        [self.dynamicsArr addObject:model];
                    }
                    [self.tableView reloadData];
                }

            }else{
                self.page --;
            }
        }
    }];
}

- (void)guanzhuWithId:(NSNumber *)ID{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"attention"];
    NSDictionary *param = @{@"toUserId":ID};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"收藏本视频 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [self getData];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}

- (NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}

- (NSMutableArray *)dynamicsArr{
    if (!_dynamicsArr) {
        _dynamicsArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dynamicsArr;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        //居左约束
        UICollectionViewFlowLayout *leftAlignedLayout = [[UICollectionViewFlowLayout alloc] init];
        leftAlignedLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 125) collectionViewLayout:leftAlignedLayout];
        _collectionView.backgroundColor = BackGroundColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;

        [_collectionView registerNib:[UINib nibWithNibName:@"CYMyFollow_TuiJianGuanZhuCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CYMyFollow_TuiJianGuanZhuCollectionViewCell"];
        
    }
    return _collectionView;
}

- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 125)];
        
        [_headView addSubview:self.collectionView];
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 25, 21, 75)];
        [btn setBackgroundImage:[UIImage imageNamed:@"标签-1"] forState:UIControlStateNormal];
        [_headView addSubview:btn];
        [_headView bringSubviewToFront:btn];
    }
    return _headView;
}

@end
